import unittest
from cvirt.cnode.libvirtXmlObject.network import *

NETWORK_XML = """
    <network>
      <name>test-nat</name>
      <forward mode="nat"/>
      <bridge name="br0" stp='on'/>
    </network>
"""

class TestNetwork(unittest.TestCase):

    def test_create_nat_network(self):
        network = NATNetwork()

        network.name = 'test-nat'
        network.address = "172.16.10.1"
        network.netmask = "255.255.255.0"
        network.dhcp_end = "172.16.10.250"
        network.dhcp_start = "172.16.10.5"
        network.dev = 'virbr0'
        self.assertEqual(network.to_xml(pretty_print=False),'<network>'
                                                                '<name>test-nat</name>'
                                                                '<bridge delay="0" stp="on"/>'
                                                                '<forward mode="nat" dev="virbr0"/>'
                                                                '<ip netmask="255.255.255.0" address="172.16.10.1">'
                                                                    '<dhcp>'
                                                                        '<range start="172.16.10.5" end="172.16.10.250"/>'
                                                                    '</dhcp>'
                                                                '</ip>'
                                                            '</network>')


    def test_create_isolate_network(self):
        n = IsolateNetwork(name='test-isolate',delay=10,mac="52:54:00:C6:0D:73",address='172.16.10.1',dhcp_end='172.16.10.250',
                           dhcp_start="172.16.10.5",netmask = "255.255.255.0")


        self.assertEqual(n.to_xml(pretty_print=False),'<network>'
                                                            '<name>test-isolate</name>'
                                                            '<bridge delay="10" stp="on"/>'
                                                            '<mac address="52:54:00:C6:0D:73"/>'
                                                            '<ip netmask="255.255.255.0" address="172.16.10.1">'
                                                                '<dhcp>'
                                                                    '<range start="172.16.10.5" end="172.16.10.250"/>'
                                                                '</dhcp>'
                                                            '</ip>'
                                                        '</network>')



    def test_create_bridge_network(self):
        n = BridgeNetwork(name='test-bridge',bridge='br0')
        self.assertEqual(n.to_xml(pretty_print=False),'<network>'
                                                          '<name>test-bridge</name>'
                                                          '<forward mode="bridge"/>'
                                                          '<bridge name="br0"/>'
                                                        '</network>')




    def test_parse_nat_network(self):
        n = NATNetwork(parse_xml='<network>'
                                    '<name>test-nat</name>'
                                    '<bridge delay="10" stp="on"/>'
                                    '<forward mode="nat"/>'
                                    '<mac address="52:54:00:C6:0D:73"/>'
                                    '<ip netmask="255.255.255.0" address="172.16.10.1">'
                                        '<dhcp>'
                                            '<range start="172.16.10.5" end="172.16.10.250"/>'
                                        '</dhcp>'
                                    '</ip>'
                                '</network>')

        self.assertEqual(n.name,'test-nat')
        self.assertEqual(n.mode,'nat')
        self.assertEqual(n.delay,'10')
        self.assertEqual(n.mac,'52:54:00:C6:0D:73')

    def test_parse_isolate_network(self):
        n = IsolateNetwork(parse_xml='<network>'
                                    '<name>test-nat</name>'
                                    '<bridge delay="10" stp="on"/>'
                                    '<mac address="52:54:00:C6:0D:73"/>'
                                    '<ip netmask="255.255.255.0" address="172.16.10.1">'
                                        '<dhcp>'
                                            '<range start="172.16.10.5" end="172.16.10.250"/>'
                                        '</dhcp>'
                                    '</ip>'
                                '</network>')

        self.assertEqual(n.name,'test-nat')
        self.assertEqual(n.mode,None)
        self.assertEqual(n.delay,'10')
        self.assertEqual(n.mac,'52:54:00:C6:0D:73')


    def test_parse_bridge_network(self):
        n = BridgeNetwork(parse_xml='''<network>
                                          <name>host-bridge</name>
                                          <forward mode="bridge"/>
                                          <bridge name="br0"/>
                                        </network>''')

        self.assertEqual(n.name,'host-bridge')
        self.assertEqual(n.mode,'bridge')
        self.assertEqual(n.bridge,'br0')

        #bridge1 is not validate
        self.assertRaises(exception.Error,BridgeNetwork,parse_xml='''<network>
                                          <name>host-bridge</name>
                                          <forward mode="bridge1"/>
                                          <bridge name="br0"/>
                                        </network>''')

